<?php

include 'user.php';

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of userManager
 *
 * @author Qbejs
 */
class userManager {
    
    private $db;
    
    public function __construct()
    {
       try
       {
          $this->db = new PDO('mysql:host=localhost;dbname=users', 'root', '');
          $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
       }
       catch(PDOException $e)
       {
          $e->errorInfo();
       }
    }
    
    public function create(User $user)
    {  
       try
       {
            #Check whether the user is registered
            $checkUser = $this->db->prepare("SELECT pesel FROM users WHERE pesel = '". $user->getPesel() ."' ");
            $this->db->execute();
            if($checkUser->rowCount() == 0)
            {
                $insertUser = $this->db->query("INSERT INTO users 
                                     (id, imie, nazwisko, adres, miasto, pesel, haslo) 
                                     VALUES
                                     ('',
                                     '".$user->getImie()."',
                                     '".$user->getNazwisko()."',
                                     '".$user->getAdres()."',
                                     '".$user->getMiasto()."',
                                     '".$user->getPesel()."',
                                     '".md5($user->getHaslo())."')");
                # Wywolanie widoku 
            }
            else
            {
                # User exist
            } 
       }
       catch(PDOException $e)
       {
           $e->errorInfo();
       }
    }
    
    public function update(User $user)
    {
        try
        {
            $updateUser = $this->db->query("UPDATE users SET
                                        imie     = '".$user->getImie()."',
                                        nazwisko = '".$user->getNazwisko()."',
                                        adres    = '".$user->getAdres()."',
                                        miasto   = '".$user->getMiasto()."',
                                        pesel    = '".$user->getPesel()."',
                                        haslo    = '".$user->getHaslo()."',
                                        WHERE id = '".$user->getId()."'");
        }
        catch(PDOException $e)
        {
            $e->errorInfo();
        }
    }
    
    public function delete(User $user)
    {
        try
        {
            $deleteUser =  $this->db->query("DELETE * FROM ussers WHERE id = '".$user->getId()."'");
        }
        catch(PDOException $e)
        {
            $e->errorInfo();
        }
    }
    
    public function findOne($param,$value)
    {

        try 
        {    
            $sql = $this->db->query("SELECT * from users WHERE ".$param." = '$value'");
            $sql->execute();
            $user = new User();
            foreach ($sql as $element)
            {
                $user->id = $element['id'];
                $user->imie = $element['imie'];
                $user->nazwisko = $element['nazwisko'];
                $user->adres = $element['adres'];
                $user->pesel = $element['pesel'];
            }

            return $user;
        }
        catch(PDOException $e)
        {
            $e->errorInfo();
        }
    }
    
    public function findAll()
    {
        try
        {
            $users = array();
            $sql = $this->db->query("SELECT * from users");
            $sql->execute();
            foreach ($sql as $element)
            {
                $user = new User();
                $user->id       = $element['id'];
                $user->imie     = $element['imie'];
                $user->nazwisko = $element['nazwisko'];
                $user->adres    = $element['adres'];
                $user->pesel    = $element['pesel'];
                array_push($users, $user);
            }
            $sql->closeCursor();
            
            return $users;
        }
        catch(PDOException $e)
        {
            $e->errorInfo();
        }
    }
    
    public function getById($id)
    {
        try
        {
            $sql = $this->db->query("SELECT * from users WHERE user_id= '$id'");
            $sql->execute();
            $user = new User();
            foreach ($sql as $element)
            {
                $user->id = $element['id'];
                $user->imie = $element['imie'];
                $user->nazwisko = $element['nazwisko'];
                $user->adres = $element['adres'];
                $user->pesel = $element['pesel'];
            }

            return $user;
        }
        catch(PDOException $e)
        {
            $e->errorInfo();
        }
    }
}

?>
